<template>
<div class="login-wrap">
		<div class="login-main">
  
			<div class="login-header">
				<div class="login-logo"><img src="../../assets/img/login-logo.png" alt=""></div>
				<div class="l-title">华风集团媒体资源运营平台</div>
			</div>
			<div class="login-cell">
				<div class="login-inner">
					<div class="l-row">
						<input type="text" placeholder="用户名" v-model="loginForm.account" class="form-control">
						<img src="../../assets/img/user.png" class="iconfont icon-artboard12"/>
					</div>
					<div class="l-row">
						<input  placeholder="密   码" type="password"  v-model="loginForm.password" class="form-control">
						<img src="../../assets/img/pwd.png" class="iconfont icon-artboard12"/>
					</div>
          	<div class="l-row" style="display:flex;align-items:center;">
           <input type="text" maxlength="4" style="width:50%;margin-right:30px;" placeholder="验证码" v-model="code" class="form-control">
            <div @click="refreshCode" style="width:60px; flex:1;">
              <s-identify :identifyCode="identifyCode"></s-identify>
            </div>
					</div>
					<div class="l-end clearfix">
						 <el-checkbox v-model="radio" size="mini">记住密码</el-checkbox>
						<!-- <div @click="findpassword" class="l-link" >找回密码</div> -->
            <el-button type="text" class="l-link" @click.native="findpassword">找回密码</el-button>
					</div>
					<div class="l-button">
						<button type="button" class="btn btn-lg btn-login" @click="handleLogin">登 录</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import Cookies from 'js-cookie'
import SIdentify from '@/components/common/Sidentify'  //**引入验证码组件**
// import { validUsername } from '@/utils/validate'
import {mapMutations} from 'vuex'
// import {Input } from 'element-ui'
export default {
  name: 'Login',
  components:{SIdentify},
  // components: { LangSelect, SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      radio:false,
    name:'',
    code:'',
    identifyCode:'',
      loginForm: {
        account: '',
        password: ''
      },
      // loginRules: {
      //   account: [{ required: true, trigger: 'blur', validator: validateUsername }],
      //   password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      // },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      identifyCodes:'123456789'
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        console.log(route)
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    // localStorage.setItem('token','9090900')
    var account=localStorage.getItem('gg-account')
    var password=localStorage.getItem('gg-password')
    if(account){
this.loginForm.account=account
    this.loginForm.password=password
    }
    
  },
  computed: {
   
  },
  mounted() {
    this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
  },
  destroyed() {
   
    
  },
  methods: {
          randomNum(min, max) {
        return Math.floor(Math.random() * (max - min) + min);
      },
     makeCode(o, l) {
        for (let i = 0; i < l; i++) {
          this.identifyCode += this.identifyCodes[
            this.randomNum(0, this.identifyCodes.length)
            ];
        }
        // console.log(this.identifyCode);
      },
    refreshCode() {
        this.identifyCode = "";
        this.makeCode(this.identifyCodes, 4);
      },
    findpassword(){
      console.log("xxxxxxxx")
this.$router.push({
  path:'/findpassword'
})
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.loading = true
      if(!this.loginForm.account){
        this.$message({
          message:'请输入用户名',
          type:'warning'
        })
        return
      }
      if(!this.loginForm.password){
        this.$message({
          message:'请输入密码',
          type:'warning'
        })
        return
      }
      if(this.identifyCode!==this.code){
         this.$message({
          message:'验证码错误!',
          type:'warning'
        })
        return
      }
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              console.log("请求成功")
              this.$router.push({path:'/'})
               if(this.radio){
        localStorage.setItem('gg-account', this.loginForm.account);
        localStorage.setItem('gg-password', this.loginForm.password);
      }
              // this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
      // this.$refs.loginForm.validate(valid => {
      //   if (valid) {
      //     this.loading = true
      //     this.$store.dispatch('user/login', this.loginForm)
      //       .then(() => {
      //         this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
      //         this.loading = false
      //       })
      //       .catch(() => {
      //         this.loading = false
      //       })
      //   } else {
      //     console.log('error submit!!')
      //     return false
      //   }
      // })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }

  }
}
</script>



<style lang="scss" scoped>
.login-wrap{
	position: fixed;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
  z-index: 9;
  // background-image: ../../assets/img/bg.jpg;
  background: url('../../assets/img/bg.jpg');
  // background-image: '../../assets/img/hdbg.jpg';
	// background: url(../../assets/img/bg.jpg) center no-repeat;
	background-size: cover;
}
.l-link{
  float: right;
  color: #062c87;
  font-size: 16px;
}
.login-header{
	position: relative;
	z-index: 1;
	text-align: center;
  padding:40px 0;
  background: url('../../assets/img/hdbg.png');
  background-position: bottom;
  background-repeat: no-repeat;
	// background: url('../../assets/img/hdbg.jpg') center bottom no-repeat;
}
.login-logo{
	margin-bottom: 30px;
}
.l-title{
	font-size: 36px;
	font-weight: bold;
	line-height: 1;
}
.login-cell{
	background-color: #FFFFFF;
	width: 540px;
	height: 420px;
	margin: 0 auto;
}
.login-inner{
	padding: 70px 75px;
	margin-top: -10px;
}
// .clearfix{
//   width: 100%;
//   display: flex;
//   justify-content: space-between;
// }
.l-row{
  position: relative;
  margin-bottom: 30px;
}
.l-row .form-control{
	border: 1px solid #d9d9d9;
	height: 50px;
	line-height: 48px;
	padding: 0 45px;
	width: 100%;
	font-size: 15px;
	box-shadow: 0 0 0 transparent;
  border-radius: 40px;
  
}
.l-row .form-control:focus{
	border: 1px solid #1168c7;
	box-shadow: 0 0 10px #83a7c8;
}
.l-row .form-control:focus+.iconfont{
	color: #1168c7;
}
.l-row .iconfont{
	display: inline-block;
	position: absolute;
	width: 30px;
	line-height: 30px;
	height: 30px;
	text-align: center;
	left: 12px;
	top: 50%;
	margin-top: -15px;
	font-size: 20px;
}
.l-row .iconfont{
	color: #999999;
}
.l-button .btn-login{
  margin-top: 30px;
	padding: 15px;
	font-size: 16px;
	background-color: #0055a4;
	background: -webkit-linear-gradient(#005ab0, #014c90); /* Safari 5.1 - 6.0 */
	background: -o-linear-gradient(#005ab0, #014c90); /* Opera 11.1 - 12.0 */
	background: -moz-linear-gradient(#005ab0, #014c90); /* Firefox 3.6 - 15 */
	background: linear-gradient(#005ab0, #014c90); /* 标准的语法 */
	box-shadow: 2px 5px 5px #83a7c8;
	width: 100%;
	border-radius: 40px;
	color: #FFFFFF;
	outline: none;
}
</style>
